import {
    request
} from "../../../request/index.js";
Page({
    data: {
        bindModal: true,
        ingredientList: [],
        ingredientNames: '',
        ingredientBook: {
            goodsName: '',
            remark: '',
            ingredientIds: '',
            taste: ''
        },
        tasteList: [
            {
                id: 1,
                name: '清淡'
            },
            {
                id: 2,
                name: '香辣'
            },{
                id: 3,
                name: '蒜香'
            }
        ]
    },
    onShow() {
        this.queryIngredient();
    },
    async queryIngredient() {
        let res = await request({
            url: "/ingredient/queryIngredient"
        })
        if (res.data.code == 200) {
            this.setData({
                ingredientList: res.data.data
            })
        }
    },
    selectFinished() {
        let {
            ingredientList,
            ingredientBook
        } = this.data;
        let ingredientNames = '';
        let ingredientIds = '';
        ingredientList.forEach(item => {
            if (item.isCheck) {
                ingredientNames += item.ingredientName + '，';
                ingredientIds += item.id + ',';
            }
        })
        if (ingredientNames.length > 0) {
            ingredientBook.goodsName = '';
            ingredientBook.remark = '';
            ingredientNames = ingredientNames.substring(0, ingredientNames.length - 1);
            ingredientIds = ingredientIds.substring(0, ingredientIds.length - 1);
            this.setData({
                bindModal: !this.data.bindModal,
                ingredientNames,
                ingredientIds,
                ingredientBook
            })
        } else {
            wx.showToast({
                title: '请选择食材',
                icon: 'error'
            })
        }
    },
    handleItemChange(e) {
        const ingredientId = e.currentTarget.dataset.id;
        let {
            ingredientList
        } = this.data;
        let index = ingredientList.findIndex(v => v.id === ingredientId);
        ingredientList[index].isCheck = !ingredientList[index].isCheck;
        this.setData({
            ingredientList
        })
    },
    bindinputGoodsName(event) {
        this.data.ingredientBook.goodsName = event.detail.value;
    },
    bindinputRemark(event) {
        this.data.ingredientBook.remark = event.detail.value;
    },
    bindinputTaste(event) {
        let _this = this;
        let tasteId = event.detail.value;
        const {tasteList} = this.data;
        tasteList.forEach(item => {
            console.log(item.id);
            if(item.id == tasteId) {
                _this.data.ingredientBook.taste = item.name;
            }
        })
    },
    async submitIngredientBook() {
        const {ingredientBook, ingredientIds} = this.data;
        ingredientBook.ingredientIds = ingredientIds;
        let res = await request({
            url: "/ingredientBook/submitIngredientBook",
            method: "POST",
            data: ingredientBook
        })
        if(res.data.code === 200) {
            wx.showToast({
              title: '订餐成功',
            })
            setTimeout(function(){
                wx.navigateTo({
                  url: '/pages/ucenter/ingredient/index',
                })
            }, 500)
        } else {
            wx.showToast({
              title: '订餐失败',
              icon: 'error'
            })
        }
    }
})